<?php
namespace Admin\Controller;
use Think\Controller;
class CourseController extends CommonController {

    //显示课程列表
    public function CourseList(){
        $course=M('course');
        $this -> CourseList = $course ->select();
        //显示编号
        $cnt = 1;
        $this -> assign('cnt',$cnt);

        $this -> display();
    }

    //显示课程详情
    public function CourseMod($course_id){
        $course=M('course');
        $this -> CourseMod = $course->select($course_id);

        //var_dump($this);
        $this -> display();
    }

    //修改课程详情
    public function doCourseMod($course_id)
    {

        $Course = M('course');
        $CourseMod = $Course->select($course_id);
        //var_dump($CourseMod);

        if (!$Course->create()) {
//             如果创建失败 表示验证没有通过 输出错误提示信息
            exit($Course->getError());
        } else {
            // 创建成功，做其他事情
            if ($Course->create()) {
                //判断是否上传了新的图片
                if ($_FILES['file']['error'] == 0) { //如果有文件上传
                    //删除之前的文件
                    unlink('./Public/upload/coursepics/' . $CourseMod['0']['c_pic']);
                    //上传新的文件
                    $pname = 'default.jpg';
                    if ($picinfo = $this->upload()) {
                        $pname = $picinfo['file']['savename'];
                    }
                    $Course->c_pic = $pname;
                }
                $Course->c_id = $course_id;
                if ($Course->save() !== false) {
                    $this->success('修改成功', './CourseList', 3);
                } else {
                    $this->error("修改失败");
                }
            }
        }
    }




    //删除课程
    public function CourseDel($course_id){

        $Course = M('course');

        $photoName = ($Course->select($course_id))['0']['c_pic'];

        if ($photoName != 'default.png') { //如果不是默认文件
            unlink('./Public/upload/coursepics/' . $photoName); //删除对应文件
        }
        if ($Course->delete($course_id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //显示添加课程界面
    public function CourseAdd()
    {
        $this->display();
    }

    //添加课程
    public function doCourseAdd()
    {
        $course = D("course");

        if (!$course->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            exit($course->getError());
        } else {
            // 创建成功，做其他事情
            if ($course->create()) {
                $pname = 'default.jpg';
                if ($picinfo = $this->upload()) {
                    $pname = $picinfo['file']['savename'];
                }
            }
            $course->c_pic = $pname;

            if ($course->add()) {
                $this->success('添加成功' ,'./courseList', 3);
            } else {
                $this->error("添加失败");
            }
        }

    }



    //文件上传upload函数
    public function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/coursepics/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $upload->saveName = time() . mt_rand(); //实际保存文件名：时间戳+随机数
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
//            $this->success('上传成功！');
            return $info;
        }
    }







}